package com.ys.web.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ys.WebConstants;
import com.ys.web.entity.User;
import com.ys.web.entity.UserRole;
import com.ys.web.pojo.MenuInfo;
import com.ys.web.pojo.MenuModule;
import com.ys.web.pojo.SystemPermissonInfo;
import com.ys.web.security.SystemPermissionUtils;
import com.ys.web.service.RoleService;
import com.ys.web.service.SystemService;

@Service
public class SystemServiceImpl implements SystemService{

	@Autowired
	RoleService roleService;
	@Override
	public void addSystemUserInfo(HttpSession session,User user) {
		
		SystemPermissonInfo systemPermissonInfo = new SystemPermissonInfo();
		
		Set<Integer> roleIds = new HashSet<Integer>();
		List<UserRole> roles ;
		try {
			roles = roleService.getRoleByUserId(user.getId());
			if(roles!=null){
				for(UserRole role: roles){
					roleIds.add(role.getRoleId());
				}
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<MenuModule> menuModules = new ArrayList<MenuModule>();
		
		if(roleIds.contains(WebConstants.SEM_USER)||roleIds.contains(WebConstants.SUPER_USER)){
			MenuModule menuModule = new MenuModule();
			menuModule.setModule(WebConstants.MODULES_SEM_WORK);
			ArrayList<MenuInfo> menuInfos = new ArrayList<MenuInfo>();
			menuInfos.addAll(WebConstants.semMenu);
			menuModule.setMenuList(menuInfos);
			menuModules.add(menuModule);
		}
		if(roleIds.contains(WebConstants.SEM_MANAGER_USER)||roleIds.contains(WebConstants.SUPER_USER)){
			MenuModule menuModule = new MenuModule();
			menuModule.setModule(WebConstants.MODULES_SEM_MANAGER);
			ArrayList<MenuInfo> menuInfos = new ArrayList<MenuInfo>();
			menuInfos.addAll(WebConstants.managerMenu);
			menuModule.setMenuList(menuInfos);
			menuModules.add(menuModule);
		
		}
		if(roleIds.contains(WebConstants.SUPER_USER)){
			MenuModule menuModule = new MenuModule();
			menuModule.setModule(WebConstants.MODULES_SEM_SUPERUSER);
			ArrayList<MenuInfo> menuInfos = new ArrayList<MenuInfo>();
			menuInfos.addAll(WebConstants.systemMenu);
			menuModule.setMenuList(menuInfos);
			menuModules.add(menuModule);
		}
//		if(roleIds.contains(WebConstants.SEM_MANAGER_USER)||roleIds.contains(WebConstants.SUPER_USER)) {
//			MenuModule menuModule = new MenuModule();
//			menuModule.setModule(WebConstants.MODULES_YWZC_SUPERUSER);
//			ArrayList<MenuInfo> menuInfos = new ArrayList<MenuInfo>();
//			menuInfos.addAll(WebConstants.ywzcMenu);
//			menuModule.setMenuList(menuInfos);
//			menuModules.add(menuModule);		
//		}
			
		
		
		systemPermissonInfo.setMenuModules(menuModules);
		
		session.setAttribute(WebConstants.SECURITY_SESSION_USERNAME_ATTR, user.getUsername());
		session.setAttribute(WebConstants.SECURITY_SESSION_USERID_ATTR, user.getId());
		
		
		systemPermissonInfo.setRoleId(roleIds);
		systemPermissonInfo.setEmployeeName(user.getEmployeeName());
		systemPermissonInfo.setEmplayeeNum(user.getEmployeeNumber());
		
		SystemPermissionUtils.addUserPermission(user.getUsername(), systemPermissonInfo);
		
	}

}
